module.exports = (EdtApi) => {
    "use strict";
    let handler, chatId = "", reg = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
    function isGroup() {
        return document.querySelectorAll('.CG20R .lzKDW')?.length > 1;
    }
    async function getChatId(times = 0) {
        let id = window.location.href?.replace("https://www.snapchat.com/web/", "")?.replace("https://web.snapchat.com/", "") || "";
        if (!reg.test(id)) id = "";
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: "nav.deg2K",
            chat: "#root div.Vbjsg.WJjwl",
            input: 'div.euyIb[contenteditable][role="textbox"]',
            msgBox: "#root",
            inMsg: "",
            outMsg: "",
            allMsg: "li.T1yt2 ul.ujRzj>li>div.KB4Aq>div.p8r1z",
            sendBtn: "button.IHV_t.v4zfr",
            setting: "div.shMO3",
            tips: {
                in: "div.ChLAv div.Titq2",
                out: "div.ChLAv div.NvRM8.vhccd>div.sT7ta",
            },
            inputType: "input",
            placeholder: {
                selector: "div.euyIb[contenteditable][role='textbox']",
                attr: "placeholder"
            },
            extra: {//其他会触发对话框变动的情况处理
                selector: "div.Vbjsg.WJjwl",
                level: 20,
                delay: 600,
            },
        },
        init(handle) {
            handler = handle;
            EdtApi.queryWait({ selector: 'nav[dir="ltr"]>ul>li>a[data-testid="navItem-CHAT"]', timeout: 60000, interval: 200 }).then(a => {
                a && a.setAttribute("target", "_self");
            });
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                this.initTips();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        initTips() {
            EdtApi.queryWait({ selector: this.config.tips.out, timeout: 60000, interval: 200 }).then(tip => {
                handler?.initTips.call(handler);
            });
        },
        getChatTitle(chatEl = document) {
            return new Promise(async (resolve) => {
                EdtApi.queryWait({ selector: "div.k1IaM div.D3ovS span.bIpkz", parent: chatEl, timeout: 10000, skip: true }).then((userEl) => {
                    resolve(userEl?.textContent || "");
                }).catch(() => {
                    resolve("");
                });
            });
        },
        getMessageList() {
            let messages = {};
            let uls = document.querySelectorAll('li.T1yt2 ul.ujRzj'), rel, litems = [], ritems = [];
            uls?.forEach(ul => {
                rel = ul.querySelector('li>header>div.GUB_w span.nonIntl');
                if (rel) {
                    ul.querySelectorAll("li>div.KB4Aq>div.p8r1z:not(.trans-inited)").forEach(item => {
                        ritems.push(item);
                    });
                } else {
                    ul.querySelectorAll("li>div.KB4Aq>div.p8r1z:not(.trans-inited)").forEach(item => {
                        litems.push(item);
                    });
                }
            });
            messages.in = litems;
            messages.out = ritems;
            console.log(messages);
            return messages;
        },
        getText(msgBox) {
            if (!msgBox) return '';
            return msgBox.querySelector("div.p8r1z>span.ogn1z")?.textContent || "";
        },
        getIptMsg(inputEl) {
            if (!inputEl) return '';
            return inputEl.textContent;
        },
        getUserInfo() {
            try {
                let avatarEl = document.querySelector("div.NavigationMenu_snapcodeContainer__eomo4>img")
                let avatar = avatarEl?.getAttribute("src") || '';
                let userNameEl = document.querySelector("h4.UserProfileInfo_h400Emphasis__EnqJk>span");
                let userName = userNameEl?.textContent;
                let accountEl = document.querySelector('h5[data-testid="username"]>span');
                let account = accountEl?.textContent;
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    };
};